local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.Server_PlayerComponent(),script.Parent.Name);

local function_list =_G.ServerOHOH.function_list

-- local TrackAttRoot=game.Workspace:WaitForChild("PVPTrack")
-- local st1 = TrackAttRoot:WaitForChild("Start01")
-- local st2 = TrackAttRoot:WaitForChild("Start02")
local RunService = game:GetService("RunService");
local testOrder = true


function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
    self.player=self.parent

    local TrackAttRoot=game.Workspace:FindFirstChild("PVPTrack")
    if not TrackAttRoot then
        print("--------没有赛道 先创建--------")
        TrackAttRoot=ServerOHOH:GetPath_Loop("PVPTrack",game.ReplicatedStorage.PVP):Clone()
        TrackAttRoot.Parent=game.Workspace
        else
            print("--------有赛道 不用创建--------")
    end
    self.TrackAttRoot = TrackAttRoot
    self.st1 = self.TrackAttRoot:WaitForChild("Start01")
    self.st2 =  self.TrackAttRoot:WaitForChild("Start02")
    -- -- self.topUi=ServerOHOH:GetPath_Loop("TopUI",game.ReplicatedStorage.Resource.UI):Clone()
    -- local track = ServerOHOH:GetPath_Loop("PVPTrack",game.Workspace)
    -- if not track then
    --     print("找不到")
    -- end
    -- -- self.Track=
end;

function M.dtor(self)

    self:ClearLevelTrack()
	M.super.dtor(self);
end;


function M.ClearPVPTrack(self)

    if self.Car then
        self.Car:dtor()
        self.Car=nil
    end
    if  self.EnemyCar then
        self.EnemyCar:dtor()
        self.EnemyCar=nil
    end

end


-- 创建一局比赛
function M.CreatePVPTrack(self)
    self.HeartbeatUpdateConnect = nil
    self:ClearPVPTrack()
    if testOrder then
        print("我方爆炸")
    else
        print("对方爆炸")
    end
    ---参数：1player，2是否是玩家乘坐的，3-7部件编号，8是否发生爆炸
    self.Car=_G.CommonOHOH.load.PVPCar().new(self.player,true,"10001_1","50001_1","20001_1","30001_1","40001_1",testOrder)
    self.EnemyCar=_G.CommonOHOH.load.PVPCar().new(self.player,false,"10001_1","50001_1","20001_1","30001_1","40001_1",not testOrder)
    
    testOrder = not testOrder

    -- local data={}
	-- for i=1,100 do
	-- 	table.insert(data,math.random(1,6))
	-- end

    -- self.ActiveLevel=_G.CommonOHOH.load.Level().new()
    self.EnemyCar:SetCFrame(self.st2.CFrame)
    self.EnemyCar:SetParent(workspace)

    self.Car:SetCFrame(self.st1.CFrame)
    self.Car:SetParent(workspace)

    if self.HeartbeatUpdateConnect then
		self.HeartbeatUpdateConnect=nil
		self.HeartbeatUpdateConnect:Disconnect()
	end
	self.HeartbeatUpdateConnect=RunService.Heartbeat:Connect(function(dt)
        
        local del = self.EnemyCar:GetCFrame().Position-self.Car:GetCFrame().Position
  
    end)
    self.Car:Start() --开始一局
    self.EnemyCar:Start()

    self.EnemyCar.PVPEndEvent:Connect(function()
        self:ClearPVPTrack()
        if self.player.main_inst then
            self.player.main_inst:LoadCharacter()
            if self.player.main_inst:FindFirstChild("ForceField") then
                self.player.main_inst.ForceField:Destroy()
            end
        end
        if self.HeartbeatUpdateConnect then
            self.HeartbeatUpdateConnect=nil
            self.HeartbeatUpdateConnect:Disconnect()
        end
    end)

    self.Car.PVPEndEvent:Connect(function()
        self:ClearPVPTrack()
        if self.player.main_inst then
            self.player.main_inst:LoadCharacter()
            if self.player.main_inst:FindFirstChild("ForceField") then
                self.player.main_inst.ForceField:Destroy()
            end
        end
        if self.HeartbeatUpdateConnect then
            self.HeartbeatUpdateConnect:Disconnect()
            self.HeartbeatUpdateConnect=nil
            
        end
    end)


end
function M.Rebirth(self)
   
    if self.player.main_inst.Character.Humanoid.Health > 0 then
        self.player.main_inst:LoadCharacter()
    end
end
M:RegistFunction(function_list.pvp_rebirth,M.Rebirth)

return M